from warnings import filterwarnings

from mock import patch
from pytest import raises

from kotti2.testing import Dummy
from kotti2.testing import DummyRequest

# filter deprecation warnings for code that is still tested...
filterwarnings("ignore", "^send_set_password is deprecated")


# noinspection PyAttributeOutsideInit
class TestSendEmail:
    def setup_method(self, method):
        get_mailer_patcher = patch("kotti2.message.get_mailer")
        get_mailer = get_mailer_patcher.start()
        self.mailer = get_mailer.return_value

        self.patchers = (get_mailer_patcher,)

    def teardown_method(self, method):
        for patcher in self.patchers:
            patcher.stop()

    def test_send_email(self, dummy_request):
        from kotti2.message import send_email

        send_email(
            dummy_request,
            ['"John Doe" <joedoe@foo.com>'],
            "kotti2:templates/email-reset-password.pt",
            {
                "site_title": "My site",
                "user_title": "John Doe",
                "url": "http://foo.com",
            },
        )

        assert self.mailer.send.called
        message = self.mailer.send.call_args[0][0]
        assert ['"John Doe" <joedoe@foo.com>'] == message.recipients
        assert "Reset your password" in message.subject

    def test_send_email_without_template_vars(self, dummy_request):
        from kotti2.message import send_email

        with raises(NameError):
            send_email(
                dummy_request,
                ['"John Doe" <joedoe@foo.com>'],
                "kotti2:templates/email-reset-password.pt",
            )


class TestSendSetPassword:
    def setup_method(self, method):
        get_settings_patcher = patch("kotti2.message.get_settings")
        self.get_settings = get_settings_patcher.start()
        self.get_settings.return_value = {
            "kotti2.site_title": "Awesome site",
            "kotti2.secret2": "123",
        }

        get_mailer_patcher = patch("kotti2.message.get_mailer")
        get_mailer = get_mailer_patcher.start()
        self.mailer = get_mailer.return_value

        self.patchers = (get_settings_patcher, get_mailer_patcher)

    def teardown_method(self, method):
        for patcher in self.patchers:
            patcher.stop()

    def test_email_set_password_basic(self, db_session):
        from kotti2.message import email_set_password

        user = Dummy(name="joe", email="joe@bar.com", title="Joe")
        email_set_password(user, DummyRequest())

        assert hasattr(user, "confirm_token")
        assert self.mailer.send.called
        message = self.mailer.send.call_args[0][0]
        assert "Your registration" in message.subject
        assert "Joe" in message.body
        assert "Awesome site" in message.body

    def test_email_set_password_add_query(self, db_session):
        from kotti2.message import email_set_password

        user = Dummy(name="joe", email="joe@bar.com", title="Joe")
        email_set_password(user, DummyRequest(), add_query={"another": "param"})

        assert self.mailer.send.called
        message = self.mailer.send.call_args[0][0]
        assert "another=param" in message.body


class TestEmailSetPassword:
    def setup_method(self, method):
        get_settings_patcher = patch("kotti2.message.get_settings")
        self.get_settings = get_settings_patcher.start()
        self.get_settings.return_value = {
            "kotti2.site_title": "Awesome site",
            "kotti2.secret2": "123",
        }

        get_mailer_patcher = patch("kotti2.message.get_mailer")
        get_mailer = get_mailer_patcher.start()
        self.mailer = get_mailer.return_value

        self.patchers = (get_settings_patcher, get_mailer_patcher)

    def teardown_method(self, method):
        for patcher in self.patchers:
            patcher.stop()

    def test_email_set_password_basic(self, db_session):
        from kotti2.message import email_set_password

        user = Dummy(name="joe", email="joe@bar.com", title="Joe")
        email_set_password(user, DummyRequest())

        assert hasattr(user, "confirm_token")
        assert self.mailer.send.called
        message = self.mailer.send.call_args[0][0]
        assert message.subject.startswith("Your registration")
        assert "Joe" in message.body
        assert "Joe" in message.html
        assert "<p" not in message.body
        assert "<p" in message.html
        assert "Awesome site" in message.body

    def test_email_set_password_other_template(self, db_session):
        from kotti2.message import email_set_password

        user = Dummy(name="joe", email="joe@bar.com", title="Joe")
        email_set_password(
            user,
            DummyRequest(),
            template_name="kotti2:templates/email-reset-password.pt",
        )

        assert self.mailer.send.called
        message = self.mailer.send.call_args[0][0]
        assert message.subject.startswith("Reset your password")

    def test_email_set_password_add_query(self, db_session):
        from kotti2.message import email_set_password

        user = Dummy(name="joe", email="joe@bar.com", title="Joe")
        email_set_password(user, DummyRequest(), add_query={"another": "param"})

        assert self.mailer.send.called
        message = self.mailer.send.call_args[0][0]
        assert "another=param" in message.body
